﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TinyERP.Framework;
using TinyERP.Framework.Controls;
using Bamboo.Core.Data;
using Bamboo.Core.Common.Collection;



namespace TinyERP.Framework.Controls
{
    public class BaseListForm : BaseForm, SimpleSearchHandler
    {
        protected IList<SearchCondition> searchConditions;
        protected SortDirection currentSortDirection;
        protected string currentSortField;
        public Panel searchPanel;
        public Panel bottomPanel;
        public Panel controlPanel;
        public Panel contentPanel;
        public Panel navigationPanel;
        public CustomDataGridView dataGridView;
        public TinyERP.Framework.Controls.DeleteButton deleteButton;
        public TinyERP.Framework.Controls.CloseButton closeButton;
        public TinyERP.Framework.Controls.NewButton newButton;
        public TinyERP.Framework.Controls.Paging paging;
        public FlowLayoutPanel controlFlowLayout;
        protected BaseSearchBar searchBar;

        public BaseListForm()
        {
            InitializeComponent();
            
            InitLocalDataVariables();
            WireEvents();
        }
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(CustomGridColumnEditor),
            typeof(System.Drawing.Design.UITypeEditor)), MergableProperty(false)]
        public DataGridViewColumnCollection DisplayColumns
        {
            get
            {
                return this.dataGridView.Columns;
            }
        }
        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BaseListForm));
            this.searchPanel = new System.Windows.Forms.Panel();
            this.bottomPanel = new System.Windows.Forms.Panel();
            this.contentPanel = new System.Windows.Forms.Panel();
            this.dataGridView = new TinyERP.Framework.Controls.CustomDataGridView();
            this.navigationPanel = new System.Windows.Forms.Panel();
            this.paging = new TinyERP.Framework.Controls.Paging();
            this.controlPanel = new System.Windows.Forms.Panel();
            this.controlFlowLayout = new System.Windows.Forms.FlowLayoutPanel();
            this.closeButton = new TinyERP.Framework.Controls.CloseButton();
            this.deleteButton = new TinyERP.Framework.Controls.DeleteButton();
            this.newButton = new TinyERP.Framework.Controls.NewButton();
            this.bottomPanel.SuspendLayout();
            this.contentPanel.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
            this.navigationPanel.SuspendLayout();
            this.controlPanel.SuspendLayout();
            this.controlFlowLayout.SuspendLayout();
            this.SuspendLayout();
            // 
            // searchPanel
            // 
            this.searchPanel.Dock = System.Windows.Forms.DockStyle.Top;
            this.searchPanel.Location = new System.Drawing.Point(0, 0);
            this.searchPanel.Name = "searchPanel";
            this.searchPanel.Size = new System.Drawing.Size(617, 37);
            this.searchPanel.TabIndex = 0;
            // 
            // bottomPanel
            // 
            this.bottomPanel.Controls.Add(this.contentPanel);
            this.bottomPanel.Controls.Add(this.controlPanel);
            this.bottomPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.bottomPanel.Location = new System.Drawing.Point(0, 37);
            this.bottomPanel.Name = "bottomPanel";
            this.bottomPanel.Size = new System.Drawing.Size(617, 261);
            this.bottomPanel.TabIndex = 1;
            // 
            // contentPanel
            // 
            this.contentPanel.Controls.Add(this.dataGridView);
            this.contentPanel.Controls.Add(this.navigationPanel);
            this.contentPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.contentPanel.Location = new System.Drawing.Point(0, 0);
            this.contentPanel.Name = "contentPanel";
            this.contentPanel.Size = new System.Drawing.Size(617, 225);
            this.contentPanel.TabIndex = 1;
            // 
            // dataGridView
            // 
            this.dataGridView.AllowUserToAddRows = false;
            this.dataGridView.AllowUserToDeleteRows = false;
            this.dataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGridView.GridStyles.Style = ComponentFactory.Krypton.Toolkit.DataGridViewStyle.List;
            this.dataGridView.GridStyles.StyleBackground = ComponentFactory.Krypton.Toolkit.PaletteBackStyle.GridBackgroundList;
            this.dataGridView.GridStyles.StyleColumn = ComponentFactory.Krypton.Toolkit.GridStyle.List;
            this.dataGridView.GridStyles.StyleDataCells = ComponentFactory.Krypton.Toolkit.GridStyle.List;
            this.dataGridView.GridStyles.StyleRow = ComponentFactory.Krypton.Toolkit.GridStyle.List;
            this.dataGridView.Location = new System.Drawing.Point(0, 0);
            this.dataGridView.ModuleAssemblyName = null;
            this.dataGridView.Name = "dataGridView";
            this.dataGridView.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.dataGridView.ReadOnly = true;
            this.dataGridView.Size = new System.Drawing.Size(617, 195);
            this.dataGridView.SortKeyMap = ((System.Collections.Hashtable)(resources.GetObject("dataGridView.SortKeyMap")));
            this.dataGridView.SortOnCurrentPage = false;
            this.dataGridView.StateCommon.BackStyle = ComponentFactory.Krypton.Toolkit.PaletteBackStyle.GridBackgroundList;
            this.dataGridView.TabIndex = 1;
            // 
            // navigationPanel
            // 
            this.navigationPanel.Controls.Add(this.paging);
            this.navigationPanel.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.navigationPanel.Location = new System.Drawing.Point(0, 195);
            this.navigationPanel.Name = "navigationPanel";
            this.navigationPanel.Size = new System.Drawing.Size(617, 30);
            this.navigationPanel.TabIndex = 0;
            // 
            // paging
            // 
            this.paging.AutoSize = true;
            this.paging.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.paging.CurrentPage = 0;
            this.paging.Dock = System.Windows.Forms.DockStyle.Fill;
            this.paging.Location = new System.Drawing.Point(0, 0);
            this.paging.Name = "paging";
            this.paging.PageSize = 2;
            this.paging.Size = new System.Drawing.Size(617, 30);
            this.paging.TabIndex = 0;
            this.paging.TotalRecord = ((long)(0));
            // 
            // controlPanel
            // 
            this.controlPanel.Controls.Add(this.controlFlowLayout);
            this.controlPanel.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.controlPanel.Location = new System.Drawing.Point(0, 225);
            this.controlPanel.Name = "controlPanel";
            this.controlPanel.Size = new System.Drawing.Size(617, 36);
            this.controlPanel.TabIndex = 0;
            // 
            // controlFlowLayout
            // 
            this.controlFlowLayout.Controls.Add(this.closeButton);
            this.controlFlowLayout.Controls.Add(this.deleteButton);
            this.controlFlowLayout.Controls.Add(this.newButton);
            this.controlFlowLayout.Dock = System.Windows.Forms.DockStyle.Fill;
            this.controlFlowLayout.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft;
            this.controlFlowLayout.Location = new System.Drawing.Point(0, 0);
            this.controlFlowLayout.Margin = new System.Windows.Forms.Padding(1);
            this.controlFlowLayout.Name = "controlFlowLayout";
            this.controlFlowLayout.Padding = new System.Windows.Forms.Padding(1);
            this.controlFlowLayout.Size = new System.Drawing.Size(617, 36);
            this.controlFlowLayout.TabIndex = 2;
            // 
            // closeButton
            // 
            this.closeButton.AutoSize = true;
            this.closeButton.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.closeButton.ButtonStyle = ComponentFactory.Krypton.Toolkit.ButtonStyle.Standalone;
            this.closeButton.Image = ((System.Drawing.Image)(resources.GetObject("closeButton.Image")));
            this.closeButton.Location = new System.Drawing.Point(555, 4);
            this.closeButton.Name = "closeButton";
            this.closeButton.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.closeButton.ResourceKey = "Close";
            this.closeButton.ResourceModuleKey = null;
            this.closeButton.Size = new System.Drawing.Size(57, 24);
            this.closeButton.TabIndex = 7;
            this.closeButton.Text = "Close";
            this.closeButton.Values.ExtraText = "Close";
            this.closeButton.Values.Image = ((System.Drawing.Image)(resources.GetObject("closeButton.Values.Image")));
            this.closeButton.Values.ImageStates.ImageCheckedNormal = null;
            this.closeButton.Values.ImageStates.ImageCheckedPressed = null;
            this.closeButton.Values.ImageStates.ImageCheckedTracking = null;
            this.closeButton.Values.Text = "";
            // 
            // deleteButton
            // 
            this.deleteButton.AutoSize = true;
            this.deleteButton.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.deleteButton.ButtonStyle = ComponentFactory.Krypton.Toolkit.ButtonStyle.Standalone;
            this.deleteButton.Image = ((System.Drawing.Image)(resources.GetObject("deleteButton.Image")));
            this.deleteButton.Location = new System.Drawing.Point(486, 4);
            this.deleteButton.Name = "deleteButton";
            this.deleteButton.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.deleteButton.ResourceKey = "Delete";
            this.deleteButton.ResourceModuleKey = null;
            this.deleteButton.Size = new System.Drawing.Size(63, 24);
            this.deleteButton.TabIndex = 6;
            this.deleteButton.Text = "Delete";
            this.deleteButton.Values.ExtraText = "Delete";
            this.deleteButton.Values.Image = ((System.Drawing.Image)(resources.GetObject("deleteButton.Values.Image")));
            this.deleteButton.Values.ImageStates.ImageCheckedNormal = null;
            this.deleteButton.Values.ImageStates.ImageCheckedPressed = null;
            this.deleteButton.Values.ImageStates.ImageCheckedTracking = null;
            this.deleteButton.Values.Text = "";
            // 
            // newButton
            // 
            this.newButton.AutoSize = true;
            this.newButton.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.newButton.ButtonStyle = ComponentFactory.Krypton.Toolkit.ButtonStyle.Standalone;
            this.newButton.Image = ((System.Drawing.Image)(resources.GetObject("newButton.Image")));
            this.newButton.Location = new System.Drawing.Point(428, 4);
            this.newButton.Name = "newButton";
            this.newButton.PaletteMode = ComponentFactory.Krypton.Toolkit.PaletteMode.Global;
            this.newButton.ResourceKey = "New";
            this.newButton.ResourceModuleKey = null;
            this.newButton.Size = new System.Drawing.Size(52, 24);
            this.newButton.TabIndex = 5;
            this.newButton.Text = "New";
            this.newButton.Values.ExtraText = "New";
            this.newButton.Values.Image = ((System.Drawing.Image)(resources.GetObject("newButton.Values.Image")));
            this.newButton.Values.ImageStates.ImageCheckedNormal = null;
            this.newButton.Values.ImageStates.ImageCheckedPressed = null;
            this.newButton.Values.ImageStates.ImageCheckedTracking = null;
            this.newButton.Values.Text = "";
            // 
            // BaseListForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(617, 298);
            this.Controls.Add(this.bottomPanel);
            this.Controls.Add(this.searchPanel);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Name = "BaseListForm";
            this.bottomPanel.ResumeLayout(false);
            this.contentPanel.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
            this.navigationPanel.ResumeLayout(false);
            this.navigationPanel.PerformLayout();
            this.controlPanel.ResumeLayout(false);
            this.controlFlowLayout.ResumeLayout(false);
            this.controlFlowLayout.PerformLayout();
            this.ResumeLayout(false);
            InitSearchPanel();
        }

        #endregion

        protected virtual void InitLocalDataVariables()
        {
            searchConditions = new List<SearchCondition>();
        }
        protected virtual void InitSearchPanel()
        {
            searchBar = new DefaultSearchBar();
            searchBar.SearchHandler = this;
            searchBar.Dock = DockStyle.Fill;
            this.searchPanel.Controls.Add(searchBar);
        }
        public override void RegisterShortcuts()
        {
            ShortcutHandler.GetInstance().RegisterShortcut(StandardShortcutKey.NEW, this, "OnNew");
            ShortcutHandler.GetInstance().RegisterShortcut(StandardShortcutKey.EDIT, this, "OnEdit");
            ShortcutHandler.GetInstance().RegisterShortcut(StandardShortcutKey.CLOSE, this, "OnClose");
            ShortcutHandler.GetInstance().RegisterShortcut(StandardShortcutKey.DELETE, this, "OnDelete");
            ShortcutHandler.GetInstance().RegisterShortcut(StandardShortcutKey.FIND, this, "OnFind");
        }

        private void WireEvents()
        {
            this.Load += delegate(object sender, EventArgs e)
            {
                OnLoad();
            };
            this.dataGridView.ColumnSortChanged += new CustomDataGridViewSortEventHandler(OnDataGridViewColumnSortChanged);
            this.paging.OnPageChanged += new PageChangedHandler(OnPageChanged);
            this.newButton.Click += delegate(object senderObj, EventArgs ev)
            {
                OnNew();
            };
            this.closeButton.Click += delegate(object senderObj, EventArgs ev)
            {
                OnClose();
            };
            this.deleteButton.Click += delegate(object senderObj, EventArgs ev)
            {
                OnDelete();
            };
            this.dataGridView.DoubleClick += delegate(object senderObj, EventArgs ev)
            {
                OnEdit();
            };
        }

        public void OnLoad()
        {
            LoadGridData();
        }

        public virtual void OnNew()
        {
        }

        public virtual void OnClose()
        {
            InvokeCloseDialog();
        }

        public virtual void OnDelete()
        {
            
        }

        public virtual void OnFind()
        {
            LoadGridData();
        }

        public virtual void OnEdit()
        {

        }

        protected virtual void BuildDefaultSearchConditions()
        {
            searchConditions = new List<SearchCondition>();
            SearchCondition condition = new SearchCondition();
            condition.SearchField = "Name";
            condition.SearchParameterName = "Name";
            condition.SearchOperator = SearchOperator.LIKE;
            string defaultSearchFieldValue = this.searchBar.SearchValue;
            condition.SearchParameterValue = "%" + defaultSearchFieldValue + "%";
            searchConditions.Add(condition);
        }

        protected virtual void LoadGridData()
        {
            string defaultSearchFieldValue = this.searchBar.SearchValue;

            if (!string.IsNullOrEmpty(defaultSearchFieldValue))
            {
                BuildDefaultSearchConditions();
            }
            if (searchConditions.Count == 0)
            {
                LoadGridViewDefaultData(currentSortDirection, currentSortField);
            }
            else
            {
                LoadGridViewSearchData(searchConditions, currentSortDirection, currentSortField);
            }
        }

        protected virtual void LoadGridViewSearchData(IList<SearchCondition> searchConditions, Bamboo.Core.Data.SortDirection sortDirection, string columnName)
        {
        }

        protected virtual void LoadGridViewDefaultData(Bamboo.Core.Data.SortDirection sortDirection, string columnName)
        {
        }

        protected virtual void OnDataGridViewColumnSortChanged(object sender, SortColumnChangedEvent e)
        {
            currentSortDirection = e.SortDirection;
            currentSortField = e.ColumnName;
            paging.CurrentPage = 0;
            LoadGridData();
        }

        protected virtual void OnPageChanged(object sender, PagingArg arg)
        {
            LoadGridData();
        }
        public override void ApplyACL(bool allowEdit, bool allowDelete, bool allowCreate)
        {
            base.ApplyACL(allowEdit, allowDelete, allowCreate);
            this.deleteButton.Enabled = allowDelete;
            this.newButton.Enabled = allowCreate;
        }
    }
}
